package com.harleyquinn.gulimall.order.service.impl;

import com.harleyquinn.gulimall.order.service.ReceiveMessageService;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
public class ReceiveMessageImpl implements ReceiveMessageService {
//    @RabbitListener(queues ="hello-java-queue" )
    @Override
    public void receiveMessage(Message message, Channel channel) throws Exception {
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        System.out.println("接收到消息--开始处理业务"+message.getBody());
        try {
            int i = 2/0;
            System.out.println("业务处理完成！");
            //手动签收、非批量签收、每条消息都要手动确认
            channel.basicAck(deliveryTag,false);
        } catch (Exception e) {
            // 拒绝签收：重新入队或者丢弃
            // long deliveryTag
            // boolean multiple 是否批量操作
            // boolean requeue第三个参数requeue=true 消息重新进入队列，broker将重新发送该消息到消费端，requeue=false 消息丢弃
            channel.basicNack(deliveryTag,false,true);
            //long deliveryTag, boolean requeue，不能批量操作
            // channel.basicReject(deliveryTag,false);
        }

    }
}
